<?php
class AuthenticationController extends Zend_Controller_Action{

	public function init(){
		parent::init();

	}

	public function indexAction(){

	}

	public function logoutAction() {
		$auth = Zend_Auth::getInstance();
		$auth->clearIdentity();
		Zend_Session::destroy();
		$baseUrl = new Zend_View_Helper_BaseUrl();
		$url = $baseUrl->getBaseUrl().'/front';
		$this->getResponse()->setRedirect($url);

	}


	public function loginAction() {

		$auth = Zend_Auth::getInstance();
		$indentify = $auth->getIdentity();
		if (isset($indentify)) {
			My_Controller_Plugin_Acl::goHomePage($this->getResponse());
		} else {
			$this->processLogin();
		}
	}

	protected function processLogin() {
		$loginForm = new Front_Form_Login();
		$this->view->form = $loginForm;
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($loginForm->isValid($formData)) {
				$username = $this->getRequest()->getParam('userName');
				$password = $this->getRequest()->getParam('passWord');
					
				if (!isset($username) || !isset($username)) {
					return;
				}
					
				$mapper = new Front_Model_UserMapper();
				$userinfo  = new Front_Model_User();
				$result = $mapper->verifyLogin($username, $password, $userinfo);

				if (isset($result)) {
					// Login success
					$roleStr = My_Controller_Plugin_Acl::getRole();
					if (isset($roleStr)) {
						$module = $roleStr;
						$baseUrl = new Zend_View_Helper_BaseUrl();
						$url = $baseUrl->getBaseUrl();
						$url = $url .'/' .$module;// .'/' .$controller .'/' .$action;
						$this->getResponse()->setRedirect($url);
					}

				} else {
					echo 'Wrong pass or username';
				}
			}
		} else {
			//			$loginForm = new Front_Form_Login();
			//			$this->view->form = $loginForm;
		}
	}
}